package com.zhxxfwzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhxxfwzx.entity.SysUser;
import com.zhxxfwzx.entity.UserRole;
import com.zhxxfwzx.mapper.SysUserMapper;
import com.zhxxfwzx.service.SysUserService;
import com.zhxxfwzx.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysUser)表服务实现类
 *
 * @author liJing
 * @since 2021-06-17 19:47:42
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    UserRoleService userRoleService;
    @Autowired
    SysUserMapper sysUserMapper;
    @Override
    public SysUser getUserByUsername(String username) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username", username);
        return getOne(qw);
    }

    @Override
    public String getUserAuthorityInfo(Long userId) {
        String authority = "";
        //角色信息
        QueryWrapper<UserRole> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);
        List<UserRole> roles = userRoleService.list(qw);

        if (roles.size() > 0){
            String roleCodes = roles.stream().map(r -> "ROLE_" + r.getRoleKey()).collect(Collectors.joining(","));
            authority = roleCodes;
        }
        //如果有菜单权限编码，就写到这里....

        return authority;
    }
}
